/*******************************************************************************
 *
 *    Copyright (c) Baina Info Tech Co. Ltd
 *
 *    AndroidSDK
 *    
 *    ColorPickerView
 *    TODO File description or class description.
 *
 *    @author: zuochen
 *    @since:  2011-9-17
 *    @version: 1.0
 *
 ******************************************************************************/

package com.sdk.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Shader;
import android.graphics.SweepGradient;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

/**
 * ColorPickerView of AndroidSDK.
 * 
 * @author zuochen
 * 
 */

public class ColorPickerView extends View {
	private  int[] mColors;
	private OnColorChangerListener mOnColorChangerListener;
	private  Paint mPaint;
	private  Paint mCenterPaint;
	/**
       * 负责监听颜色更改
       *
       */
	public interface OnColorChangerListener {
		void onColorChanged(int color);
	}
	public ColorPickerView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	public ColorPickerView(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public ColorPickerView(Context context) {
		super(context);
	}

	public ColorPickerView(Context c, int color,
			OnColorChangerListener colorChangerListener) {
		super(c);
		this.mOnColorChangerListener = colorChangerListener;
		mColors = new int[] { 0xFFFF0000, 0xFFFF00FF, 0xFF0000FF,
				0xFF00FFFF, 0xFF00FF00, 0xFFFFFF00, 0xFFFF0000 };
		final Shader s = new SweepGradient(0, 0, mColors, null);
		//初始化画板
		mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mPaint.setShader(s);
		mPaint.setStyle(Paint.Style.STROKE);
		mPaint.setStrokeWidth(32);
		//初始化中心画板
		mCenterPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
		mCenterPaint.setColor(color);
		mCenterPaint.setStrokeWidth(5);
	}
	private boolean mTrackingCenter;
	private boolean mHighlightCenter;
	private static final int CENTER_X = 100;
	private static final int CENTER_Y = 100;
	private static final int CENTER_RADIUS = 32;
	@Override
	protected void onDraw(Canvas canvas) {
		final float r = CENTER_X - mPaint.getStrokeWidth() * 0.5f;

		canvas.translate(CENTER_X, CENTER_X);

		canvas.drawOval(new RectF(-r, -r, r, r), mPaint);
		canvas.drawCircle(0, 0, CENTER_RADIUS, mCenterPaint);

		if (mTrackingCenter) {
			final int c = mCenterPaint.getColor();
			mCenterPaint.setStyle(Paint.Style.STROKE);

			if (mHighlightCenter) {
				mCenterPaint.setAlpha(0xFF);
			} else {
				mCenterPaint.setAlpha(0x80);
			}
			canvas.drawCircle(0, 0,
					CENTER_RADIUS + mCenterPaint.getStrokeWidth(),
					mCenterPaint);

			mCenterPaint.setStyle(Paint.Style.FILL);
			mCenterPaint.setColor(c);
		}
	}
	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		setMeasuredDimension(CENTER_X * 2, CENTER_Y * 2);
	}
//	private int floatToByte(float x) {
//		final int n = java.lang.Math.round(x);
//		return n;
//	}
//
//	private int pinToByte(int n) {
//		if (n < 0) {
//			n = 0;
//		} else if (n > 255) {
//			n = 255;
//		}
//		return n;
//	}

	private int ave(int s, int d, float p) {
		return s + java.lang.Math.round(p * (d - s));
	}

	private int interpColor(int colors[], float unit) {
		if (unit <= 0) {
			return colors[0];
		}
		if (unit >= 1) {
			return colors[colors.length - 1];
		}

		float p = unit * (colors.length - 1);
		final int i = (int) p;
		p -= i;
		// now p is just the fractional part [0...1) and i is the index
		final int c0 = colors[i];
		final int c1 = colors[i + 1];
		final int a = ave(Color.alpha(c0), Color.alpha(c1), p);
		final int r = ave(Color.red(c0), Color.red(c1), p);
		final int g = ave(Color.green(c0), Color.green(c1), p);
		final int b = ave(Color.blue(c0), Color.blue(c1), p);

		return Color.argb(a, r, g, b);
	}
	private static final float PI = 3.1415926f;

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		final float x = event.getX() - CENTER_X;
		final float y = event.getY() - CENTER_Y;
		final boolean inCenter = java.lang.Math.sqrt(x * x + y * y) <= CENTER_RADIUS;

		switch (event.getAction()) {
		case MotionEvent.ACTION_DOWN:
			mTrackingCenter = inCenter;
			if (inCenter) {
				mHighlightCenter = true;
				invalidate();
				break;
			}
		case MotionEvent.ACTION_MOVE:
			if (mTrackingCenter) {
				if (mHighlightCenter != inCenter) {
					mHighlightCenter = inCenter;
					invalidate();
				}
			} else {
				final float angle = (float) java.lang.Math.atan2(y, x);
				// need to turn angle [-PI ... PI] into unit [0....1]
				float unit = angle / (2 * PI);
				if (unit < 0) {
					unit += 1;
				}
				mCenterPaint.setColor(interpColor(mColors, unit));
				invalidate();
			}
			break;
		case MotionEvent.ACTION_UP:
			if (mTrackingCenter) {
				if (inCenter) {
					if (null!=mOnColorChangerListener) {
						mOnColorChangerListener.onColorChanged(mCenterPaint.getColor());
					}
				}
				mTrackingCenter = false; // so we draw w/o halo
				invalidate();
			}
			break;
		}
		return true;
	}

}
